package com.etrans.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 经纬度距离运算工具
 * 
 * @author 劳
 * @version 1.0
 * @since 2015-12-16
 * description
 * <p>创建类 -2015-12-16</p> 
 */
public class DistanceUtil {
	
	/**
	 * 地球半径
	 */
	private static double EARTH_RADIUS = 6378.137;
	
	private static int DISTANCEUNIT = 1000;
	
	/**
	 * getRad
	 * 
	 * @param d double
	 * @return double
	 */
	private static double rad(double d){
	   return d * Math.PI / 180.0;
	}


	/**
	 * 获取两点的直线距离
	 * 
	 * @param lat1 double
	 * @param lng1 double
	 * @param lat2 double
	 * @param lng2 double
	 * @return Integer 两点间的距离
	 */
	public static double GetDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 1000;
		return s;
	}
	
	/**
	 * 获取两点的直线距离
	 * 
	 * @param lat1 double
	 * @param lng1 double
	 * @param lat2 double
	 * @param lng2 double
	 * @return Integer 两点间的距离
	 */
	public static Integer GetDistance(int lat1, int lng1, int lat2, int lng2) {
		 double lat1_ = Double.parseDouble(String.valueOf(lat1))/1000000;
		 double lng1_ = Double.valueOf(String.valueOf(lng1))/1000000;
		 double lat2_ = Double.valueOf(String.valueOf(lat2))/1000000;
		 double lng2_ = Double.valueOf(String.valueOf(lng2))/1000000;
		 double distance = GetDistance(lat1_,lng1_,lat2_,lng2_);
		 return  Integer.valueOf(String.valueOf(Math.round(distance)));
	}
 
	
	/**
	 * <p>根据经纬度串运算多点经纬度的总里程</p>
	 * 
	 * @param latlonStr   经纬度串
	 * @param split1            分割每隔经纬度的分隔符
	 * @param split2            分割经度跟纬度的分隔符
	 * @param maxDistance 两点间的最大距离
	 * 
	 * @return distanceAll Integer 返回最后运算的距离[开始经纬度至结束经纬度间的距离]
	 */
	public static Integer GetDistance(String latlonStr, String split1,
			String split2, int maxDistance,int precision) {
		if (StringUtils.isEmpty(latlonStr)){
			return 0;
		}
		//总里程，开始经度，开始纬度
		Integer distanceAll = 0, starLat = 0, starlon = 0,invalidlatlon = 0; 
		String[] latlonAry = latlonStr.split(split1);
		// 球两点间的距离
		for (int i = 0; i < latlonAry.length; i++) {
			String[] latlon = latlonAry[i].split(split2);
			if (i == 0) {
				starLat = Integer.parseInt(latlon[1]);
				starlon = Integer.parseInt(latlon[0]);
				continue;
			}
			Integer currentLat = Integer.parseInt(latlon[1]);
			Integer currentLon = Integer.parseInt(latlon[0]);
			Integer distance = GetDistance(starLat, starlon, currentLat, currentLon);
			if(distance<=0)continue;
			// 里程转为km double
			double distance_d_km = Double.parseDouble(String.valueOf(distance))/precision;
			// 将最大的点距转为 double
			double maxDistance_km = Double.parseDouble(String.valueOf(maxDistance*(invalidlatlon+1)))/DISTANCEUNIT;
			if(distance_d_km>maxDistance_km){//非法距离直接舍弃该点，跳至下一个点
				invalidlatlon++;
				continue;
			}
			//无效经纬度统计重置
			invalidlatlon=0;
			// 里程求和
			distanceAll+=distance;
			starLat = currentLat;
			starlon = currentLon;
		}
		return distanceAll;
	}
	
	
	public static void main(String[] args){
//		String latlon = "23125589,113328175;23127103,113328078";
//		Integer allDistance = GetDistance(latlon,";",",",700,10);
//		System.out.println(allDistance);
		//System.out.println(Double.parseDouble(String.valueOf(23113689))/1000000);
		int distance = GetDistance(23120142,113328366,23129705,113327911);
		System.out.println(distance);
	}
}
